package com.hustzp.com.xichuangzhu.poetry.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.poetry.model.UserNotification;
import com.hustzp.com.xichuangzhu.utils.ImageUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * Copyright (C) 2015 The huimai mobile client Project
 * All right reserved.
 *
 * @author: wuzhimin@huimai365.com
 * @date: 2016/5/28 0:06
 * @Description:通知列表页适配器
 */
public class NotificationListAdapter extends BaseAdapter {
    private Context context;
    private List<UserNotification> collectionses;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public NotificationListAdapter(Context context, List<UserNotification> collectionses) {
        this.context = context;
        this.collectionses = collectionses;
    }

    @Override
    public int getCount() {
        return collectionses.size();
    }

    @Override
    public UserNotification getItem(int position) {
        return collectionses.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context,
                    R.layout.activity_notification_list_item, null);
            holder.initView(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        bindViews(position, holder);
        return convertView;
    }

    private void bindViews(int position, ViewHolder holder) {
        UserNotification collections = getItem(position);
        ImageUtils.loadImage(collections.getUSER().getAVFile("avatar").getUrl(), holder.imgAvatar);
        holder.tv_username.setText(collections.getUSER().getUsername());
//        holder.tv_upvotesCount.setText(collections.getUPVOTESCOUNT());
        holder.tv_comment_content.setText(collections.getCONTENT());
        holder.tv_create_time.setText(getTime(collections.getCreatedAt()));

    }

    private String getTime(Date sdate) {
        String result = sdf.format(sdate);
        int month = 0;
        int day = 0;
        int hour = 0;
        int minute = 0;
        int second = 0;
        try {
            Date d1 = new Date();//当前时间
            Date d2 = sdate;//传进的时间
            long cha = d1.getTime() - d2.getTime();
            month = getMonths(d2, d1);
            day = new Long(cha / (1000 * 60 * 60 * 24)).intValue();
            hour = new Long(cha / (1000 * 60 * 60)).intValue();
            minute = new Long(cha / (1000 * 60)).intValue();
            second = new Long(cha / 1000).intValue();
            if (month >= 12) {
                result = new Long(month / 12).intValue() + "年";
            } else if (month > 0 && month < 12) {
                result = month + "个月";
            } else if (day > 0) {
                result = day + "天";
            } else if (hour > 0) {
                result = hour + "小时";
            } else if (minute > 0) {
                result = minute + "分钟";
            } else if (second > 0) {
                result = "刚刚";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    private int getMonths(Date date1, Date date2) {
        int month;
        Calendar cal1 = new GregorianCalendar();
        cal1.setTime(date1);
        Calendar cal2 = new GregorianCalendar();
        cal2.setTime(date2);
        month = (cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR)) * 12
                + cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH);
        return month;
    }

    class ViewHolder {
        ImageView imgAvatar;
        TextView tv_username;
        TextView tv_upvotesCount;
        TextView tv_comment_content;
        TextView tv_create_time;

        public void initView(View view) {
            imgAvatar = (ImageView) view
                    .findViewById(R.id.imgAvatar);
            tv_username = (TextView) view
                    .findViewById(R.id.tv_username);
            tv_upvotesCount = (TextView) view
                    .findViewById(R.id.tv_upvotesCount);
            tv_comment_content = (TextView) view
                    .findViewById(R.id.tv_comment_content);
            tv_create_time = (TextView) view
                    .findViewById(R.id.tv_create_time);

        }
    }
}
